package com.aphrodite.ui.widgets;

import javax.microedition.lcdui.Graphics;

/**
 * A Typing text effect
 * 
 * @author kentelt
 * 
 */
public class TypingText extends TextEffect {

	protected char[] charList;

	protected volatile int index;

	public TypingText(Theme theme, String text) {
		super(theme, 0, 0);
		this.text = text;
		updateModel();
	}

	protected void doAniTask() {
		if (index < charList.length) {
			index++;
		} else
			index = 0;
	}

	protected void drawTypedText(Graphics g, int color) {
		g.setColor(color);
		if (theme.bmFont == null) {
			g.setFont(theme.widgetFont);
			g.drawChars(charList, 0, index, 2, 2, 0);
		} else {
			theme.bmFont.drawChars(g, charList, 0, index, 2, 2, 0);
		}
	}

	public void render(Graphics g) {
		if (!isVisible)
			return;
		capture(g, width, height, true);
		drawTypedText(g, theme.fgColor);
		release(g, width, height, true);

	}

	protected void updateModel() {
		width = (theme.bmFont == null) ? theme.widgetFont.stringWidth(text) : theme.bmFont.stringWidth(text);
		height = (theme.bmFont == null) ? theme.widgetFont.getHeight() : theme.bmFont.getHeight();
		width += 4;
		height += 4;
		charList = text.toCharArray();
		index = 0;
	}
}
